/*
 * Copyright 2009 Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.appengine.demos.oauth;

import com.google.appengine.api.urlfetch.HTTPHeader;
import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;

import java.io.IOException;

/**
 * OAuth authenticated HTTP fetches.
 */
public class OAuthFetchService {
  private final URLFetchService fetchService;
  
  public OAuthFetchService() {
    this(URLFetchServiceFactory.getURLFetchService());
  }

  public OAuthFetchService(URLFetchService fetchService) {
    this.fetchService = fetchService;
  }

  OAuthFetchResponse fetch(HTTPRequest request, OAuthFetchOptions options) throws IOException {
    if (options.oauthAccessTokenUrl != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_access_token_url", options.oauthAccessTokenUrl));
    }
    if (options.oauthReceivedCallbackUrl != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_received_callback_url", options.oauthReceivedCallbackUrl));
    }
    if (options.oauthConsumerSecret != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_consumer_secret", options.oauthConsumerSecret));
    }
    if (options.oauthAddEmail != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_add_email", options.oauthAddEmail));
    }
    if (options.oauthState != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_state", options.oauthState));
    }
    if (options.oauthRequestTokenSecret != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_request_token_secret", options.oauthRequestTokenSecret));
    }
    if (options.oauthTokenName != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_token_name", options.oauthTokenName));
    }
    if (options.oauthConsumerKey != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_consumer_key", options.oauthConsumerKey));
    }
    if (options.oauthUseToken != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_use_token", options.oauthUseToken));
    }
    if (options.oauthDesiredCallbackUrl != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_desired_callback_url", options.oauthDesiredCallbackUrl));
    }
    if (options.oauthRequestMethod != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_request_method", options.oauthRequestMethod));
    }
    if (options.oauthRequestTokenUrl != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_request_token_url", options.oauthRequestTokenUrl));
    }
    if (options.oauthAuthorizationUrl != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_authorization_url", options.oauthAuthorizationUrl));
    }
    if (options.oauthSignUser != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_sign_user", options.oauthSignUser));
    }
    if (options.oauthServiceName != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_service_name", options.oauthServiceName));
    }
    if (options.oauthRequestToken != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_request_token", options.oauthRequestToken));
    }
    if (options.oauthParamLocation != null) {
      request.setHeader(new HTTPHeader(
          "x_oauth_param_location", options.oauthParamLocation));
    }
    return new OAuthFetchResponse(fetchService.fetch(request));
  }
}
